package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.minio.propreties.MinioProperties;
import com.atguigu.gmall.product.service.FileUploadService;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * 文件上传功能-控制器
 * @Author 卡尼奶
 * @Date 2022/5/18 19:20
 * @Version 1.0
 */
@Service
public class FileUploadServiceImpl implements FileUploadService {

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinioProperties minioProperties;

    @Override
    public String fileUpload(MultipartFile file) throws Exception {
        String filename = System.currentTimeMillis() + "_" + file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();
        PutObjectOptions options = new PutObjectOptions(inputStream.available(),-1);
        options.setContentType(file.getContentType());
        minioClient.putObject(minioProperties.getBucketName(), filename,inputStream ,options );
        String path = minioProperties.getEndpoint()+minioProperties.getBucketName()+"/"+filename;
        return path;
    }
}
